import 'package:flutter/material.dart';
import 'package:jubobao/other/Widgets/JuBoCommonWidget.dart';
import 'package:jubobao/profile/JuBoProfileServices.dart';
import 'package:jubobao/profile/approval/JuBoApprovalList.dart';
import 'package:jubobao/profile/approval/JuBoApprovalReport.dart';

class JuBoApproval extends StatefulWidget {

  static final String routeName = 'approval';

  @override
  _JuBoApprovalState createState() => _JuBoApprovalState();
}

class _JuBoApprovalState extends State<JuBoApproval> {

  List _result;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    JuBoApprovalServices.approvalRequest().then((value){
        print(value[0].icon);
        setState(() {
            _result = value;
        });
    });

  }

  @override
  Widget build(BuildContext context) {

    Widget widget;

    if(_result == null){
      widget =  JuBoWidget.loadingWidget();
    }else{
      widget = Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            approvalTop(context),
            approvalBottom()
          ],
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Text('我的审批'),
      ),
      body: widget
    );
  }

  Widget  approvalBottom(){
    return Container(
      padding: EdgeInsets.zero,
      child: GridView.builder(
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        itemCount: _result.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: 5,
            crossAxisSpacing: 5
        ),
        itemBuilder: (ctx,index){
           return Card(
            child:
                approvalMenuNet(_result[index].icon, _result[index].name, () {
              print('点击上报');

              Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
                return JuBoApprovalReport();
              }));
            }),
          );
        },
      ),
    );
  }

  Widget  approvalTop(ctx){
    return Container(
      height: 100,
      child: Card(
        color: Colors.white,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            approvalTopInfoMenu('wodeshenpi_icon','我的审批',(){
              Navigator.of(ctx).pushNamed(JuBoApprovalList.routeName);
            }),
            approvalTopInfoMenu('icon-wdsp-zhihui','我的知会',(){}),
            approvalTopInfoMenu('wodeshenqing_icon','我的申请',(){}),
          ],
        ),
      ),
    );
  }

  Widget approvalTopInfoMenu(String menuIcon,String title,Function f){
    return InkWell(
      child: Container(
        color:Colors.white ,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.asset('assets/profile/$menuIcon.png',width: 25,),
            SizedBox(height: 5,),
            Text(title)
          ],
        ),
      ),
      onTap: f,
    );
  }

  Widget approvalMenuNet(String menuIcon,String title,Function f){
    return InkWell(
      child: Container(
        color:Colors.white ,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.network(menuIcon,width: 25,),
            SizedBox(height: 5,),
            Text(title)
          ],
        ),
      ),
      onTap: f,
    );
  }

}
